package com.epfresh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.utils.T;
import com.epfresh.bean.Address;
import com.epfresh.bean.CityAddressBox;
import com.epfresh.bean.HomeCity;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class CityAddressSelectedAdapter extends BaseAdapter implements View.OnClickListener {

    public interface OnCitySelectListener {
        public void onCitySelect(int position, int childPosition, String cityName, String cityId, String lng, String lat, String addressId, boolean isLocation);
    }

    public Context context;


    private List<CityAddressBox> list;
    private OnCitySelectListener onCitySelectListener;

    public CityAddressSelectedAdapter(Context context, List<CityAddressBox> list) {
        this.context = context;
        this.list = list;
    }

    public OnCitySelectListener getOnCitySelectListener() {
        return onCitySelectListener;
    }

    public void setOnCitySelectListener(OnCitySelectListener onCitySelectListener) {
        this.onCitySelectListener = onCitySelectListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        CityAddressBox cityAddressBox = list.get(position);
        return cityAddressBox.getType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int type = getItemViewType(position);

        //type == 0(当前收货地址名称),1(地址item),2(开通城市),3(具体城市)
        if (type == 0) {
            return getAddressTopView(position, convertView, parent);
        }
        if (type == 1) {
            return getAddressView(position, convertView, parent);
        }
        if (type == 2) {
            return getCityTopView(position, convertView, parent);
        }
        if (type == 3) {
            return getCityView(position, convertView, parent);
        }

        return convertView;
    }


    public View getAddressTopView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_city_address, null, false);
            vh.tvFlag = (TextView) convertView.findViewById(R.id.tv_flag);
            vh.tvContent = (TextView) convertView.findViewById(R.id.tv_content);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

    public View getAddressView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_city_address_detail, null, false);
            vh.tvFlag = (TextView) convertView.findViewById(R.id.tv_flag);
            vh.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
            vh.tvContent.setLines(2);
            vh.tvFlag.setVisibility(View.INVISIBLE);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        CityAddressBox cityAddressBox = list.get(position);
        Address address = cityAddressBox.getAddress();
        if (address != null) {
            vh.tvContent.setText(address.getDetailAddressStr());
            if (address.isSelect()) {

                vh.tvFlag.setVisibility(View.VISIBLE);
                vh.tvContent.setTextColor(context.getResources().getColor(R.color.green_little));
            } else {
                vh.tvFlag.setVisibility(View.INVISIBLE);
                vh.tvContent.setTextColor(Color.parseColor("#686868"));
            }
        } else {
            vh.tvFlag.setVisibility(View.INVISIBLE);
            vh.tvContent.setTextColor(Color.parseColor("#686868"));
            vh.tvContent.setText("");
        }

        vh.tvContent.setTag(R.id.item_key_position, position);
        vh.tvContent.setTag(R.id.item_key_view, -1);
        vh.tvContent.setTag(R.id.item_key_status, cityAddressBox);
        vh.tvContent.setOnClickListener(this);
        return convertView;
    }

    public View getCityTopView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_city_location, null, false);
            vh.tvContent = (TextView) convertView.findViewById(R.id.tv_city);
            vh.vUnopened = convertView.findViewById(R.id.rl_unopened);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        CityAddressBox cityAddressBox = list.get(position);
        vh.tvContent.setText(cityAddressBox.getLocation());

        vh.tvContent.setTag(R.id.item_key_position, position);
        vh.tvContent.setTag(R.id.item_key_view, -2);
        vh.tvContent.setTag(R.id.item_key_status, cityAddressBox);
        vh.tvContent.setOnClickListener(this);
        String cityId1 = cityAddressBox.getCityId();
        String cityName1 = cityAddressBox.getCityName();
        if ((cityId1 == null || "".equals(cityId1)) && (cityName1 != null && !"".equals(cityName1))) {//没有定位到
            vh.vUnopened.setVisibility(View.VISIBLE);
        } else {
            vh.vUnopened.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    public View getCityView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_city_opened, null, false);
            vh.tvFlag = (TextView) convertView.findViewById(R.id.tv_flag);
            vh.tvContent = (TextView) convertView.findViewById(R.id.tv_city_0);
            vh.tvContent1 = (TextView) convertView.findViewById(R.id.tv_city_1);
            vh.tvContent2 = (TextView) convertView.findViewById(R.id.tv_city_2);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        CityAddressBox cityAddressBox = list.get(position);
        List<HomeCity> cityList = cityAddressBox.getCityList();

        int size = cityList.size();
        vh.tvContent.setVisibility(View.INVISIBLE);
        vh.tvContent1.setVisibility(View.INVISIBLE);
        vh.tvContent2.setVisibility(View.INVISIBLE);

        vh.tvContent.setTag(R.id.item_key_position, position);
        vh.tvContent.setTag(R.id.item_key_view, -1);
        vh.tvContent.setTag(R.id.item_key_status, cityAddressBox);
        vh.tvContent1.setTag(R.id.item_key_position, position);
        vh.tvContent1.setTag(R.id.item_key_view, -1);
        vh.tvContent1.setTag(R.id.item_key_status, cityAddressBox);
        vh.tvContent2.setTag(R.id.item_key_position, position);
        vh.tvContent2.setTag(R.id.item_key_view, -1);
        vh.tvContent2.setTag(R.id.item_key_status, cityAddressBox);

        vh.tvContent.setClickable(false);
        vh.tvContent1.setClickable(false);
        vh.tvContent2.setClickable(false);

        vh.tvContent.setOnClickListener(this);
        vh.tvContent1.setOnClickListener(this);
        vh.tvContent2.setOnClickListener(this);
        if (size > 0) {
            HomeCity homeCity = cityList.get(0);
            vh.tvContent.setVisibility(View.VISIBLE);
            vh.tvContent.setText(homeCity.getName());
            vh.tvContent.setClickable(true);
            vh.tvContent.setTag(R.id.item_key_view, 0);
            changeBg(vh.tvContent, homeCity.isSelect());
        }
        if (size > 1) {
            HomeCity homeCity = cityList.get(1);
            vh.tvContent1.setVisibility(View.VISIBLE);
            vh.tvContent1.setText(homeCity.getName());
            vh.tvContent1.setClickable(true);
            vh.tvContent1.setTag(R.id.item_key_view, 1);
            changeBg(vh.tvContent1, homeCity.isSelect());
        }
        if (size > 2) {
            HomeCity homeCity = cityList.get(2);
            vh.tvContent2.setVisibility(View.VISIBLE);
            vh.tvContent2.setText(homeCity.getName());
            vh.tvContent2.setClickable(true);
            vh.tvContent2.setTag(R.id.item_key_view, 2);
            changeBg(vh.tvContent2, homeCity.isSelect());
        }

        return convertView;
    }

    public void changeBg(TextView tv, boolean isSelect) {
        if (isSelect) {
            tv.setBackgroundResource(R.drawable.icon_city_select);
            tv.setTextColor(context.getResources().getColor(R.color.green));
        } else {
            tv.setBackgroundResource(R.drawable.normal_city_bg);
            tv.setTextColor(Color.parseColor("#686868"));
        }
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        Object o1 = v.getTag(R.id.item_key_status);
        if (o != null && o instanceof Integer && o1 != null && o1 instanceof CityAddressBox) {
            if (onCitySelectListener != null) {
                int position = (int) o;

                o = v.getTag(R.id.item_key_view);

                int childPosition = -1;
                if (o != null && o instanceof Integer) {
                    childPosition = (int) o;
                }
                CityAddressBox cityAddressBox = (CityAddressBox) o1;
                int type = cityAddressBox.getType();
                //type == 0(当前收货地址名称),1(地址item),2(开通城市),3(具体城市)
                String cityName = "";
                String cityId = "";
                String lng = null;
                String lat = null;
                String addressId = null;
                boolean isLocation = false;
                if (type == 1) {
                    Address address = cityAddressBox.getAddress();
                    if (address != null) {
                        cityName = address.getCityName();
                        cityId = address.getCityId();
                        lng = address.getLng();
                        lat = address.getLat();
                        addressId = address.getId();
                    }
                } else if (type == 2) {
                    isLocation = true;
                    String cityId1 = cityAddressBox.getCityId();
                    String cityName1 = cityAddressBox.getCityName();

                    if ((cityId1 == null || "".equals(cityId1)) && (cityName1 == null || "".equals(cityName1))) {//没有定位到
                        T.toast(cityAddressBox.getLocation());
                        return;
                    }
                    if ((cityId1 == null || "".equals(cityId1)) && (cityName1 != null && !"".equals(cityName1))) {//没有定位到
                        //不在开通城市
                        T.toast("当前位置不在开通区域");
                        return;
                    }

                    cityName = cityName1;
                    cityId = cityId1;

                } else if (type == 3) {
                    List<HomeCity> cityList = cityAddressBox.getCityList();
                    if (cityList != null && childPosition < cityList.size()) {
                        HomeCity homeCity = cityList.get(childPosition);
                        cityName = homeCity.getName();
                        cityId = homeCity.getCode();
                    }
                }
                onCitySelectListener.onCitySelect(position, childPosition, cityName, cityId, lng, lat, addressId, isLocation);

            }
        }
    }

    public class ViewHolder {
        TextView tvFlag;
        TextView tvContent;
        TextView tvContent1;
        TextView tvContent2;
        View vUnopened;
    }
}
